//
//  HTEmotionPackageModel.swift
//  表情键盘
//
//  Created by whtapple on 4/3/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit

class HTEmotionPackageModel: NSObject {
    //MARK -模型属性
    //id
    var id: String
    //组名
    var group_name_cn: String
    //所有的表情
    var emoticons: [HTEmotionModel]
    //分页表情汇总
    var pageemotions: [[HTEmotionModel]] = [[HTEmotionModel]]()
    //MARK -初始化
    init(id: String, group_name_cn: String, emoticons: [HTEmotionModel]) {
       
        self.id = id
        self.group_name_cn = group_name_cn
        self.emoticons = emoticons
         super.init()
        loadPageemotions(emoticons)
    }
    //给分页表情赋值
    private func loadPageemotions(emoticons: [HTEmotionModel]){
        //总表情个数
        let count = emoticons.count
        //根据表情个数算出总页数
        let pageCount = (count + pageemotionCount - 1) / pageemotionCount
        //当页数为0时，自己创建一个数组，为什么
        if pageCount == 0 {
            let emptyemotions = [HTEmotionModel]()
            pageemotions.append(emptyemotions)
        }
        //进行分页创建
        for i in 0..<pageCount{
            //每个数组长度
            var length = pageemotionCount
            //位置
            let location = i * pageemotionCount
            //当超过时，说明没有这么多个了
            if  location + pageemotionCount > count{
                length = count - location
            }
            //计算出每页的范围
            let range = NSRange(location: location, length: length)
            //得到每个分页数组
            let spliteEmotions = (emoticons as NSArray).subarrayWithRange(range) as! [HTEmotionModel]
            pageemotions.append(spliteEmotions)
        }
        
    }
    //描述
    override var description: String{
        return "\n\t PackageModel模型id:\(id),group_name_en:\(group_name_cn),emoticons:\(emoticons),pageemotions:\(pageemotions)"
    }
}
